<template>
  <div class="admin_page">
    <admin-header></admin-header>
    <admin-side-bar></admin-side-bar>
    <admin-container>
      <el-form inline size="mini" :model="form">
        <el-form-item>
          <el-input
            v-model="form.name"
            prefix-icon="el-icon-search"
            placeholder="标题"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="form.cate_id"
            placeholder="分类"
            filterable
            clearable
          >
            <el-option
              v-for="(item, idx) in options"
              :key="idx"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="getData">
            搜索
          </el-button>
          <el-button type="primary" @click="() => $refs.add.show()">
            添加
          </el-button>
          <el-button type="primary" @click="() => $refs.cache.show()">
            更新数据
          </el-button>
          <el-button
            type="primary"
            @click="
              () =>
                $router.push({
                  path: '/admin/cache',
                  query: { key: 'news', from: $route.fullPath }
                })
            "
          >
            清理缓存
          </el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="appLoading"
        border
        size="mini"
        :data="data.data"
        row-key="id"
      >
        <el-table-column label="ID" prop="id"></el-table-column>
        <el-table-column label="缩略图">
          <template slot-scope="{ row }">
            <el-image :src="row.thumb" style="height: 40px"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="标题" width="300px">
          <template slot-scope="{ row }">
            <a :href="'/home/news/detail?id=' + row.id" target="_blank">{{
              row.name
            }}</a>
          </template>
        </el-table-column>
        <el-table-column label="分类" prop="cate_name"></el-table-column>
        <el-table-column label="来源" prop="comefrom"></el-table-column>
        <el-table-column label="发布者" prop="author_name"></el-table-column>
        <el-table-column label="发布时间" prop="ctime"></el-table-column>
        <el-table-column
          show-overflow-tooltip
          label="摘要"
          prop="abstract"
        ></el-table-column>
        <el-table-column label="页数" prop="content_page"></el-table-column>
        <el-table-column label="状态" prop="status"></el-table-column>
        <el-table-column label="排序" prop="sort"></el-table-column>
        <el-table-column fixed="right" label="操作" width="200px">
          <template slot-scope="{ row }">
            <el-button
              type="text"
              size="mini"
              @click="$refs.cache.show(row.id)"
            >
              更新数据
            </el-button>
            <el-button type="text" size="mini" @click="$refs.add.show(row)">
              修改
            </el-button>
            <el-button type="text" size="mini" @click="del(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="mt10 bg_fff">
        <el-pagination
          :total="page.total"
          :page-size="page.pageSize"
          :current-page="page.pageNum"
          @current-change="pageChange"
        ></el-pagination>
      </div>
    </admin-container>
    <news-add-dialog
      ref="add"
      @ok="id => $refs.cache.show(id)"
    ></news-add-dialog>
    <news-cache-update-dialog
      ref="cache"
      @ok="refresh"
    ></news-cache-update-dialog>
  </div>
</template>

<script>
import AdminHeader from "../../../components/admin/header";
import AdminSideBar from "../../../components/admin/side-bar";
import AdminContainer from "../../../components/admin/container";
import { AppLoadingMixin } from "../../../lib/mixins/app-loading";
import { PageMixin } from "../../../lib/mixins/PageMixin";
import NewsAddDialog from "../../../components/news/add-dialog";
import NewsCacheUpdateDialog from "../../../components/news/cache-dialog";

export default {
  name: "NewsList",
  components: {
    NewsCacheUpdateDialog,
    NewsAddDialog,
    AdminContainer,
    AdminSideBar,
    AdminHeader
  },
  mixins: [AppLoadingMixin, PageMixin],
  data() {
    return {
      options: [],
      form: { fid: 0, module: "news" }
    };
  },
  computed: {},
  async mounted() {
    const { query = {} } = this.$route;
    if (query.cate_id) this.$set(this.form, "cate_id", parseInt(query.cate_id));
    this.options = await this.loadOptions();
    this.getData();
  },
  methods: {
    del(data) {
      this.$confirm(`是否删除${data.name}?`).then(async () => {
        if (await this.$newsService.del(data.id + "")) {
          this.calculatePage("-");
          this.getData();
        }
      });
    },

    async loadOptions(pageNum = 1, options = []) {
      const data = await this.$cateService.list({
        module: "news",
        pageNum
      });
      options = options.concat(data.data);
      if (options.length !== data.count)
        // eslint-disable-next-line no-return-await
        return await this.loadOptions(++pageNum, options);
      return options;
    },

    async getData() {
      const { page, form } = this;
      this.data.data.splice(0);
      this.data = await this.$newsService.list({ ...page, ...form });
    }
  }
};
</script>

<style scoped></style>
